/* tree.h for !DiscEx */


/*
 * The ForDRec (File or Directory Rec) is the basic component of the in-core
 *  representation of the directory tree.
 *
 * Each record contains information about one object:
 *
 *  v - identifies the object's position on the disc, and contains a flag
 *       to say whether it is a directory or a file
 *  u - contains the length of a file, or the address of the ForDRec for the
 *       first entry if it is a directory
 * 
 * ForDRec's for siblings are normally held contiguously, and the last one is
 *  followed immediately by a ForDPtr which addresses their common parent (or
 *  NULL if there is no parent). Another flag in the v field indicates which
 *  record is the last one in a sequence.
 *
 * The 'haslink' flag bit is set whenever an ForDRec (that is not the last
 *  one for a directory) is not immediately followed by its sibling. In such
 *  a case, the word immediately following the record contains an ForDPtr to
 *  the sibling.
 */

typedef struct _ford
{
    IDA v;
    union
    {
        struct _ford *son;
        unsigned len;
    } u;
    char name[0];    /* leafname of file or directory */
} ForDRec, *ForDPtr;


/*
 * Access to the information in an ForDRec from other modules should always
 *  be via the following macros and functions (and never by direct access
 *  to the fields of the structure).
 */

#define  tree_get_name(ford)    (ford)->name
#define  tree_isdir(ford)       (ford)->v.isdir
#define  tree_get_len(ford)     (ford)->u.len      /* valid for file only */
#define  tree_get_fragid(ford)  (ford)->v.fragid
#define  tree_get_sector(ford)  (ford)->v.sector

extern ForDPtr tree_get_parent (ForDPtr entry);
extern error * tree_walk
(
    ScanListPtr p,
    error * (*f) (ForDPtr entry, void *handle),
    void *handle
);


/*
 * These routines handle the creation of the representation of the
 *  hierarchy in memory.
 */

extern error * tree_continue_scan (ScanListPtr pp, Bool *complete);
extern char * tree_scan_position (ScanListPtr pp);
extern error * tree_delete_scan (ScanListPtr pp);
extern error * tree_init_scan (ScanListPtr pp);


/* used by the 'locate' module only */

extern error * tree_next_dir_entry
(
    char *dir,        /* directory name */
    int *item,        /* set this to zero on first call */
    Bool *found,      /* set TRUE iff an entry was found */
    char *name,       /* the object's name is copied here */
    unsigned *size,   /* the object's length is copied here */
    IDA *sin,         /* the object's internal disc address is copied here */
    int *type         /* and its type (1 or 2) is placed here */
);
